package com.playball.lottery.frm.yhzx;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.playball.lottery.ApplicationData;
import com.playball.lottery.JddMainActivity;
import com.playball.lottery.R;
import com.playball.lottery.customview.MyDialog;
import com.playball.lottery.customview.ShowCustomDialog;
import com.playball.lottery.customview.ShowTipDialog;
import com.playball.lottery.nt.GetRequestParams;
import com.playball.lottery.util.AbstractActivity;
import com.playball.lottery.util.AppUtils;
import com.playball.lottery.util.Constants;
import com.playball.lottery.util.ConvertJsonToList;
import com.playball.lottery.util.DefConstants;
import com.playball.lottery.util.Logger;
import com.playball.lottery.util.LotUtil;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 充值
 * 
 * @ClassName: JddRechargeListActivity
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author vanszhen
 * @date 2014年6月3日 下午4:47:08
 * 
 */
public class JddRechargeListActivity extends AbstractActivity implements DefConstants {
	View alipay_recharge;
	private TextView tv_title;
	private TextView ibtn_title_left;

	/** TextView 用户名 */
	private TextView txtUserName;
	/** TextView 账号余额 */
	private TextView txtUserBalance;
	/** EditText 输入的金额 */
	private EditText etRechargeMoney;

	private String ordernum;

	private static String TAG = "Jdd_Alipay";

	ProgressDialog mProgress = null;

	static String PAY_SUCCESS = "{9000}";

	private View um_recharge;
	private View lianlian_recharge;
	private View um_recharge_tip;
	private View szf_recharge ;
	
	private int HongBaoID;

	private String balance;

	private View jdd_recharge;
	private View jdd_jmjf_lott;
	private View jdd_jmjf_bet;
	private View jdd_jmjf_zbs;

	private ImageView jmjf_lott_icon;
	private TextView jmjf_lott_name;
	private TextView jmjf_lott_issue;
	private TextView jmjf_lott_zs;
	private TextView jmjf_lott_bs;
	private TextView jmjf_lott_money;

	// 账户余额
	private TextView accountBalance;
	// 至少支付
	private TextView laestPay;
	// 输入的金额
	// private EditText pay_rec_edt ;
	// 金额
	private TextView pay_rec_txt;
	// 是否使用余额
	private CheckBox check_balance_pay;
	// 是fou
	private boolean flagBalance = true;

	private String getStrMoney;

	private String result;
	// 一共需要支付
	private TextView txt_total_money;
	// 红包 金额
	private String recharge;

	private String payDescribe;
	/** 判断是否从推送通知过来 */
	private Boolean ifPush;
	
	/** 判断onResume第一次执行 */
	private Boolean firstResume = false;
	Handler handler = new Handler() {
		public void handleMessage(Message msg) {

			switch (msg.what) {
			case 0X008:
				closeProgress();
				txtUserBalance.setText(balance);
				ApplicationData.getInstance().setUserMoney(balance);
				showOper();
				break;

			case 0X009:
				asyLoad();
				break;
			case 0X010:
				showBuySuccess();
				break;
			default:
				break;
			}

		};

	};
	private RelativeLayout kj_alipay_recharge;

	// 操作
	private void showOper() {
		ShowCustomDialog.Builder tDialog = new ShowCustomDialog.Builder(JddRechargeListActivity.this);
		tDialog.setTitle("温馨提示");
		tDialog.setMessage("操作成功");
		tDialog.setNegativeButton("确定", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				setResult(RESULT_OK);
				dialog.cancel();
				finish();
			}

		}).createCustomDialog().show();
	}

	// 购买成功
	private void showBuySuccess() {
		ShowCustomDialog.Builder tDialog = new ShowCustomDialog.Builder(JddRechargeListActivity.this);
		tDialog.setTitle("购买成功");
		tDialog.setMessage("购买成功，祝您中奖!");
		tDialog.setNegativeButton("返回首页", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Intent intent = new Intent();
				intent.setClass(context, JddMainActivity.class);
				context.startActivity(intent);

				// EventManager.getInstance().onEvent(EventType.EV_UI_FRM_MAIN_JUMP,
				// FrmWorkStatus.MAINHOME);
				setResult(RESULT_OK);
				finish();
			}

		}).setPositiveButton("查看投注记录", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Intent intent = new Intent(JddRechargeListActivity.this, BetRecordActivity.class);
				startActivity(intent);
				setResult(RESULT_OK);
				finish();
			}
		})/*
		 * .setNeutralButton("继续购彩", new DialogInterface.OnClickListener() {
		 * 
		 * @Override public void onClick(DialogInterface dialog, int which) {
		 * 
		 * 
		 * 
		 * } })
		 */
		.createCustomDialog().show();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.jdd_rechargelist_new);
		initView();
		HongBaoID = 0;
		firstResume = true;
		Intent intent = getIntent();
		recharge = intent.getStringExtra("recharge");
		ifPush = intent.getBooleanExtra("ifPush", false);
		// String adId=intent.getStringExtra("adEnabled");//获取EditText是否编辑
		String balance = intent.getStringExtra("showBalance");// 显示优惠余额

		// Log.d("van", "balance--"+balance);
		/*
		 * if("1".equals(adId)) {
		 * 
		 * }
		 */
		if (recharge == null || "".equals(recharge)) {
			recharge = "10";
		} else {
			if ("100".equals(recharge))
				HongBaoID = 1;
			else if ("1000".equals(recharge))
				HongBaoID = 2;
			else if ("98".equals(recharge))
				HongBaoID = 3;
			else if ("998".equals(recharge))
				HongBaoID = 4;
			else if ("9998".equals(recharge))
				HongBaoID = 5;
			else if ("29998".equals(recharge))
				HongBaoID = 6;
			else if ("59998".equals(recharge))
				HongBaoID = 7;
			else if ("99998".equals(recharge))
				HongBaoID = 8;

			if (HongBaoID == 1 || HongBaoID == 2) {
				payDescribe = "抢红包充值";
				tv_title.setText("抢红包充值");
				um_recharge.setVisibility(View.GONE);
				lianlian_recharge.setVisibility(View.GONE);
				um_recharge_tip.setVisibility(View.VISIBLE);
				szf_recharge.setVisibility(View.GONE);
			} else if (HongBaoID >= 3 && HongBaoID <= 8) {
				payDescribe = "充值抢红包";
				tv_title.setText("充值抢红包");
				um_recharge.setVisibility(View.VISIBLE);

				lianlian_recharge.setVisibility(View.VISIBLE);

				um_recharge_tip.setVisibility(View.GONE);
				findViewById(R.id.hongbao_recharge_tip).setVisibility(View.VISIBLE);

				findViewById(R.id.et_recharge_money).setVisibility(View.GONE);
				findViewById(R.id.showtable).setVisibility(View.VISIBLE);

				((TextView) findViewById(R.id.showMoney)).setText(Html.fromHtml("<font color='#ff6668'>" + recharge
						+ "</font>彩豆 "));
				((TextView) findViewById(R.id.showBalance)).setText(Html.fromHtml("送<font color='#ff6668'>" + balance
						+ "</font>彩豆 红包"));

			}

		}
		etRechargeMoney.setText(recharge);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		AsyLoadMoney2 money = new AsyLoadMoney2();
		String[] parms = new String[2];
		JSONObject params = new JSONObject();
		parms[1] = params.toString();
		parms[0] = "107";
		money.execute(parms);
	}

	/*
	 * 初始化view
	 */
	private void initView() {
		// TODO Auto-generated method stub

		tv_title = (TextView) findViewById(R.id.top_title_info);
		ibtn_title_left = (TextView) findViewById(R.id.top_title_back);
		txtUserName = (TextView) findViewById(R.id.user_name);
		txtUserName.setText(ApplicationData.getInstance().getUserName());
		txtUserBalance = (TextView) findViewById(R.id.user_balance);
		txtUserBalance.setText(ApplicationData.getInstance().getUserMoney());
		etRechargeMoney = (EditText) findViewById(R.id.et_recharge_money);
		um_recharge_tip = findViewById(R.id.um_recharge_tip);

		jdd_recharge = findViewById(R.id.jdd_recharge);
		jdd_jmjf_lott = findViewById(R.id.jdd_jmjf_lott);
		jdd_jmjf_bet = findViewById(R.id.jdd_jmjf_bet);
		jdd_jmjf_zbs = findViewById(R.id.jdd_jmjf_zbs);
		szf_recharge = findViewById(R.id.szf_recharge);
		szf_recharge.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent  intent = new Intent(context, ShenzhoufuActivity.class);
				startActivityForResult(intent, 10000);
			}
		});
		

		jmjf_lott_icon = (ImageView) findViewById(R.id.jmjf_lott_icon);
		jmjf_lott_name = (TextView) findViewById(R.id.jmjf_lott_name);
		jmjf_lott_issue = (TextView) findViewById(R.id.jmjf_lott_issue);
		jmjf_lott_zs = (TextView) findViewById(R.id.jmjf_lott_zs);
		jmjf_lott_bs = (TextView) findViewById(R.id.jmjf_lott_bs);
		jmjf_lott_money = (TextView) findViewById(R.id.jmjf_lott_money);
		txt_total_money = (TextView) findViewById(R.id.jmjf_lott_total_money);

		accountBalance = (TextView) findViewById(R.id.jmjf_lott_account_balance);
		laestPay = (TextView) findViewById(R.id.jmjf_lott_laest_pay);
		// pay_rec_edt = (EditText) findViewById(R.id.jmjf_lott_pay_rec_edt);
		pay_rec_txt = (TextView) findViewById(R.id.jmjf_lott_pay_rec_txt);
		check_balance_pay = (CheckBox) findViewById(R.id.jmjf_lott_balance_pay);
		check_balance_pay.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if (check_balance_pay.isChecked()) {
					flagBalance = isChecked;
					// initData();
					needChangeData(1);
				} else {
					flagBalance = isChecked;
					// initData();
					needChangeData(0);
				}
			}
		});

		result = getIntent().getStringExtra("params");
		Logger.e(TAG, "result = " + result);

		if (TextUtils.isEmpty(result)) {
			payDescribe = "充值";
			tv_title.setText("充值");
			jdd_recharge.setVisibility(View.VISIBLE);
			jdd_jmjf_lott.setVisibility(View.GONE);
			jdd_jmjf_bet.setVisibility(View.GONE);
			jdd_jmjf_zbs.setVisibility(View.GONE);
		} else {
			payDescribe = "在线支付";
			tv_title.setText("在线支付");
			jdd_recharge.setVisibility(View.GONE);
			jdd_jmjf_lott.setVisibility(View.VISIBLE);
			jdd_jmjf_bet.setVisibility(View.VISIBLE);
			jdd_jmjf_zbs.setVisibility(View.VISIBLE);
			initData();
		}

		// 支付宝
		alipay_recharge = findViewById(R.id.alipay_recharge);
		kj_alipay_recharge = (RelativeLayout) findViewById(R.id.kj_alipay_recharge);
		alipay_recharge.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

			}
		});
		kj_alipay_recharge.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (TextUtils.isEmpty(result)) {
					checkAlipayORUMValue("alipaykj_mobile");
				} else {
					checkAlipayORUMValueJMJF("alipaykj_mobile");
				}
			}
		});
		ibtn_title_left.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});

		// U付
		um_recharge = findViewById(R.id.um_recharge);
		um_recharge.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (TextUtils.isEmpty(result)) {
					checkAlipayORUMValue("Um_mobile");
				} else {
					checkAlipayORUMValueJMJF("Um_mobile");
				}
			}
		});

		// 连连
		lianlian_recharge = findViewById(R.id.lianlian_recharge);
		lianlian_recharge.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				if (TextUtils.isEmpty(ApplicationData.getInstance().getRealName())
						|| TextUtils.isEmpty(ApplicationData.getInstance().getIDCardNumber())) {
					MyDialog.Builder dialog = new MyDialog.Builder(JddRechargeListActivity.this);
					dialog.setTitle("温馨提示");
					dialog.setMessage("使用无网银支付，必须绑定个人身份信息，是否立即完善？");
					dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							// 获得自定义的应用程序BallDTO,初始化
							Intent intent = new Intent(JddRechargeListActivity.this, ScPersonActivity.class);
							startActivity(intent);
							dialog.cancel();
						}
					});
					dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.cancel();
						}
					}).create().show();
					return;
				}

				if (TextUtils.isEmpty(result)) {
					checkAlipayORUMValue("lianlian_mobile");
				} else {
					checkAlipayORUMValueJMJF("lianlian_mobile");
				}
			}
		});
	}

	private void initData() {

		try {

			JSONTokener jsonParser = new JSONTokener(result);
			JSONObject js = (JSONObject) jsonParser.nextValue();

			jmjf_lott_icon.setBackgroundResource(LotUtil.getIconByID(js.getInt("LotteryID")));

			jmjf_lott_name.setText(LotUtil.getLotNamebyID(js.getInt("LotteryID")));

			txt_total_money.setText(Html.fromHtml(String.format("共需支付：<font color='#ff6666'>%1$d</font>彩豆",
					js.getInt("Money"))));

			String issuename = "";

			try {
				issuename = js.getString("IssueName");
			} catch (Exception e) {
				issuename = js.getString("IssueID");
			}

			// jmjf_lott_issue.setText("第" + issuename + "期");
			jmjf_lott_issue.setText(Html.fromHtml(String.format("第 <font color='#ff6666'>%s</font>期", issuename)));

			if ("-1".equals(issuename) || js.getInt("LotteryID") == LOT_ID_JCZQ
					|| js.getInt("LotteryID") == LOT_ID_JCLQ)
				jmjf_lott_issue.setVisibility(View.INVISIBLE);

			int zhuijia = 2;
			try {
				zhuijia = js.getInt("zhuijia");
			} catch (Exception e) {
			}

			jmjf_lott_bs.setText(Html.fromHtml(String.format("倍数：<font color='#ff6666'>%1$d</font>倍",
					js.getInt("Multiple"))));
			jmjf_lott_money.setText(Html.fromHtml(String.format("需要支付：<font color='#ff6666'>%1$d</font>彩豆",
					js.getInt("Money"))));
			jmjf_lott_zs.setText(Html.fromHtml(String.format("购买注数：<font color=red>%1$d</font>注", js.getInt("Money")
					/ js.getInt("Multiple") / zhuijia)));

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	// 需要改变
	private void needChangeData(final int flag) {

		try {
			JSONTokener jsonParser = new JSONTokener(result);
			JSONObject js = (JSONObject) jsonParser.nextValue();
			final double accBalance = Double.parseDouble(balance);
			final int buyMoney = js.getInt("Money");
			final int leastPay = (int) Math.ceil(buyMoney - accBalance);
			if (flagBalance) {
				if (accBalance == 0) {
					// Log.d("van", accBalance+"-----尼玛啥意思");
					// laestPay.setText(Html.fromHtml(String.format("至少支付：<font color='#ff6666'>%1$d</font>彩豆",
					// js.getInt("Money"))));
					// pay_rec_edt.setText(js.getInt("Money")+"");
					pay_rec_txt.setText(js.getInt("Money") + "");
					getStrMoney = js.getInt("Money") + "";

					laestPay.setText(Html.fromHtml(String.format("还需支付       ：<font color='#ff6666'>%1$d</font>彩豆",
							js.getInt("Money"))));
				} else {
					// Log.d("van", accBalance+"+++++ 草拟大爷");
					// laestPay.setText(Html.fromHtml(String.format("至少支付：<font color='#ff6666'>%1$.2f</font>彩豆",
					// buyMoney-accBalance)));
					int doubleTointMoney = (int) Math.ceil(buyMoney - accBalance);
					// pay_rec_edt.setText(doubleTointMoney+"");
					pay_rec_txt.setText(doubleTointMoney + "");
					getStrMoney = doubleTointMoney + "";

					laestPay.setText(Html.fromHtml(String.format("还需支付       ：<font color='#ff6666'>%1$d</font>彩豆",
							doubleTointMoney)));
				}
			} else {
				// laestPay.setText(Html.fromHtml(String.format("至少支付：<font color='#ff6666'>%1$d</font>彩豆",
				// js.getInt("Money"))));
				// pay_rec_edt.setText(js.getInt("Money")+"");
				pay_rec_txt.setText(js.getInt("Money") + "");
				getStrMoney = js.getInt("Money") + "";
				laestPay.setText(Html.fromHtml(String.format("还需支付       ：<font color='#ff6666'>%1$d</font>彩豆",
						js.getInt("Money"))));
			}
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	// 对复选框的 监听
	OnCheckedChangeListener listener = new OnCheckedChangeListener() {
		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			if (check_balance_pay.isChecked()) {
				flagBalance = isChecked;
				// initData();
				needChangeData(1);
			} else {
				flagBalance = isChecked;
				// initData();
				needChangeData(0);
			}
		}
	};

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		
		if (Constants.ZNZH_RES_CODE == resultCode) {
			String tip = data.getExtras().getString("szfcancle");
			Log.d("van", "ZNZH_RES_CODE");
			showLLDilog(tip);
		}
		
		
		if (20008 == resultCode) {
			//Log.d("van", "JddRechargeListActivity--");
			 JddRechargeListActivity.this.finish();
		}
		 if (requestCode == 100000) {
			 closeProgress();
	            super.onActivityResult(requestCode, resultCode, data);
	        }
	}

	public static final String RET_CODE_SUCCESS = "0000";

	// 异步加载
	private void asyLoad() {
		AsyLoadMoney money = new AsyLoadMoney();
		String[] parms = new String[2];
		JSONObject params = new JSONObject();
		parms[1] = params.toString();
		parms[0] = "1071";
		money.execute(parms);
	}

	/***
	 * 
	 * @ClassName: AsyLoadMoney
	 * @Description: TODO(这里用一句话描述这个类的作用)
	 * @author vanszhen
	 * @date 2014年6月23日 下午2:55:41
	 * 
	 */
	class AsyLoadMoney extends AsyncTask<String, Integer, String> {
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
		}

		@Override
		protected String doInBackground(String... params) {
			String strResult;
			String action = params[0];
			strResult = GetRequestParams.getRequestUserIDResult(params[1], ApplicationData.getInstance().getUserID(),
					action, getApplicationContext());
			return strResult;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			// { "Code": 0, "Data": "{\r\n  \"Balance\": \"60.27\"\r\n}"}
			try {
				if (result != null) {
					JSONTokener jsonParser = new JSONTokener(result);
					JSONObject js = (JSONObject) jsonParser.nextValue();
					if (Integer.parseInt(js.getString("Code")) == 0) {
						List<Map<String, Object>> maps = ConvertJsonToList.getM(js.getString("Data"));
						balance = maps.get(0).get("Balance").toString();
						if (TextUtils.isEmpty(ApplicationData.getInstance().getUserMoney())
								&& Float.parseFloat(balance) > 0
								|| Float.parseFloat(balance) >= Float.parseFloat(ApplicationData.getInstance()
										.getUserMoney())) {
							handler.sendEmptyMessage(0X008);
						} else {
							handler.sendEmptyMessageDelayed(0X009, 1000);
						}

					}
				} else {
					showToast("暂无数据！");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

	class AsyLoadMoney2 extends AsyncTask<String, Integer, String> {

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
		}

		@Override
		protected String doInBackground(String... params) {
			String strResult;
			String action = params[0];
			strResult = GetRequestParams.getRequestUserIDResult(params[1], ApplicationData.getInstance().getUserID(),
					action, getApplicationContext());
			return strResult;
		}

		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			// { "Code": 0, "Data": "{\r\n  \"Balance\": \"60.27\"\r\n}"}
			try {
				if (result != null) {
					JSONTokener jsonParser = new JSONTokener(result);
					JSONObject js = (JSONObject) jsonParser.nextValue();
					if (Integer.parseInt(js.getString("Code")) == 0) {

						Map<String, Object> map = new HashMap<String, Object>();
						map = ConvertJsonToList.getMap(js.getString("Data"));

						try {
							ApplicationData.getInstance().setRealName(map.get("RealName").toString());
							ApplicationData.getInstance().setBankId(Integer.parseInt(map.get("BankID").toString()));
							ApplicationData.getInstance().setCardNum(map.get("BankCardNumber").toString());
							ApplicationData.getInstance().setMobile(map.get("Mobile").toString());
							ApplicationData.getInstance().setBankName(map.get("BankName").toString());
							ApplicationData.getInstance().setUserMoney(map.get("Balance").toString());
							ApplicationData.getInstance().setIDCardNumber(map.get("IDCardNumber").toString());
							ApplicationData.getInstance().setIsMobileValied(
									map.get("IsMobileValied").toString().equals("true"));
							ApplicationData.getInstance().setSecurityQuestion(map.get("SecurityQuestion").toString());
							ApplicationData.getInstance().setIsPayPwd(Integer.parseInt(map.get("IsPayPwd").toString()));
							ApplicationData.getInstance().setRegisterTime(map.get("RegisterTime").toString());

							balance = map.get("Balance").toString();
							txtUserBalance.setText(balance);
							accountBalance.setText(Html.fromHtml(String.format(
									"账户余额：<font color='#ff6666'>%1$.2f</font>彩豆", Double.parseDouble(balance))));

//							alipayCheckAccount();
						} catch (Exception e) {

						}

					}
				} else {
					showToast("暂无数据！");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}

	// 支付宝前置
//	private void alipayCheckAccount() {
//		if (!TextUtils.isEmpty(result)) {
//			needChangeData(1);
//		}
//		if (firstResume && (!TextUtils.isEmpty(result) || (HongBaoID >= 3 && HongBaoID <= 8))) {
//			firstResume = false;
//			CheckAccountTask checkTask = new CheckAccountTask(JddRechargeListActivity.this, new OnCheckListener() {
//				@Override
//				public void check(boolean arg0) {
//					if (arg0) {
//						if (TextUtils.isEmpty(result)) {
//							checkAlipayORUMValue("alipaykj_mobile");
//						} else {
//							checkAlipayORUMValueJMJF("alipaykj_mobile");
//						}
//					}
//				}
//			});
//			checkTask.checkAccountIfExist();
//		}
//	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	@Override
	public void finish() {
		// TODO Auto-generated method stub
		super.finish();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (ifPush) {
				Intent intent = new Intent(this, JddMainActivity.class);
				startActivity(intent);
			}
			this.finish();
			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}





	// 关闭进度框
	void closeProgress() {
		try {
			if (mProgress != null) {
				mProgress.dismiss();
				mProgress = null;
			}
		} catch (Exception ex) {
			Log.e("error", ex.toString());
		}
	}

	/*************************** 银行卡充值*******************2014年6月4日11:05:07 ***********************************/

	/**
	 * 0 真实姓名和身份证都不空 1、姓名不为空 3、都空
	 */
	int kind = 0;

	public boolean isNeedWriteInfo() {
		// System.out.println(ApplicationData.getInstance().getRealName()+"  "+
		// ApplicationData.getInstance().getIDCardNumber());
		if (!TextUtils.isEmpty(ApplicationData.getInstance().getRealName())
				&& !TextUtils.isEmpty(ApplicationData.getInstance().getIDCardNumber())) {
			// if(!TextUtils.equals("",
			// ApplicationData.getInstance().getRealName()) &&
			// !TextUtils.equals("",
			// ApplicationData.getInstance().getIDCardNumber())){
			kind = 0;
			return false;
			// }else if(false == TextUtils.equals("",
			// ApplicationData.getInstance().getRealName())){
		} else if (false == TextUtils.isEmpty(ApplicationData.getInstance().getRealName())) {
			kind = 1;
			MyDialog.Builder dialog = new MyDialog.Builder(JddRechargeListActivity.this);
			dialog.setTitle("完善信息提示");
			dialog.setMessage("您还没有绑定身份证信息，现在去用户中心绑定吗？");
			dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.cancel();
					// 跳转到用户中心
					Intent intent = new Intent(JddRechargeListActivity.this, SecurityCenterActivity.class);
					startActivity(intent);
				}
			});
			dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.cancel();
					finish();
				}
			}).create().show();
			return true;
		} else {
			kind = 2;
			return false;
		}
	}

	public void startSdkToPay(String _ordernum) {
		// TODO Auto-generated method stub
		Log.e(TAG, "kind = " + kind);
		String tradeNo = _ordernum;
		if (kind == 3) {
//			UmpayQuickPay.requestPayWithBind(JddRechargeListActivity.this, tradeNo, null, "1", null, null, 100000);
		} else {
//			UmpPayInfoBean infoBean = new UmpPayInfoBean();
//			infoBean.setCardHolder(ApplicationData.getInstance().getRealName());
//			infoBean.setEditFlag("0");
//			infoBean.setIdentityCode(ApplicationData.getInstance().getIDCardNumber());
//			UmpayQuickPay.requestPayWithBind(JddRechargeListActivity.this, tradeNo, null, "1", null, infoBean, 100000);
		}

	}

	/************************ 公用方法 *************************************************/

	private void checkAlipayORUMValue(String tpye) {

		if (HongBaoID == 1 || HongBaoID == 2 || HongBaoID == 0) {
			Log.d("van", "充值----" + etRechargeMoney.getText().toString().trim());
			if (AppUtils.isBlank(etRechargeMoney.getText().toString().trim())) {
				Toast.makeText(getApplicationContext(), R.string.enter_your_top_up_amount, Toast.LENGTH_SHORT).show();
				return;
			}
			try {
				String money = etRechargeMoney.getText().toString();

				if (false == isNeedWriteInfo()) {

					if (Integer.parseInt(money) >= 1) {

						AsyncLoader113 task = new AsyncLoader113(tpye, money);
						// 支付宝 在线充值
						task.execute("108");

					} else {
						Toast.makeText(getApplicationContext(), R.string.amount_should_be_greater_than_six_yuan,
								Toast.LENGTH_SHORT).show();
						return;
					}

				}
			} catch (Exception e) {
				Toast.makeText(getApplicationContext(), R.string.top_up_amount_is_wrong, Toast.LENGTH_SHORT).show();
			}
		} else {
			Log.d("van", "充值红包----" + recharge);
			if (AppUtils.isBlank(recharge)) {
				Toast.makeText(getApplicationContext(), R.string.enter_your_top_up_amount, Toast.LENGTH_SHORT).show();
				return;
			}
			try {
				String money = recharge;

				if (false == isNeedWriteInfo()) {

					if (Integer.parseInt(money) >= 1) {

						AsyncLoader113 task = new AsyncLoader113(tpye, money);
						// 支付宝 在线充值
						task.execute("1082");

					} else {
						Toast.makeText(getApplicationContext(), R.string.amount_should_be_greater_than_six_yuan,
								Toast.LENGTH_SHORT).show();
						return;
					}

				}
			} catch (Exception e) {
				Toast.makeText(getApplicationContext(), R.string.top_up_amount_is_wrong, Toast.LENGTH_SHORT).show();
			}

		}

	}

	/**
	 * 异步请求
	 */
	private ShowTipDialog showdialog = new ShowTipDialog();

	// 调用奖多多的接口
	public class AsyncLoader113 extends AsyncTask<String, Integer, String> {// 继承AsyncTask

		private String typeBankPay;
		private String money;

		public AsyncLoader113(String typeBankPay, String money) {
			super();
			this.typeBankPay = typeBankPay;
			this.money = money;
		}

		// 1.准备运行
		protected void onPreExecute() {
			showdialog.showDialog(JddRechargeListActivity.this);
		}

		// 2.正在后台运行
		@Override
		protected String doInBackground(String... params) {
			String strResult;
			try {
				// 设置请求所需参数
				JSONObject param = new JSONObject();
				param.put("BankPay", typeBankPay);
				param.put("PayMoney", money);
				if (HongBaoID != 0) {
					param.put("HongBaoID", HongBaoID);
					if (HongBaoID == 1 || HongBaoID == 2) {
						param.put("activeType", 1);
					} else {
						param.put("activeType", 2);
					}

				}
				// 设置请求action
				String action = params[0];
				String UserID = ApplicationData.getInstance().getUserID();
				strResult = GetRequestParams.getRequestUserIDResult(param.toString(), UserID, action,
						getApplicationContext());
				return strResult;
			} catch (Exception ex) {
				// TODO: handle exception
				Log.e("error", ex.toString());
				return null;
			}
		}

		// 3.进度更新
		protected void onProgressUpdate(Integer... progress) {
		}

		// 4.完成后台任务
		protected void onPostExecute(String result) {
			if (result != null) {

				// System.out.println("result = " + result);

				try {
					JSONTokener jsonParser = new JSONTokener(result);
					JSONObject js = (JSONObject) jsonParser.nextValue();
					if (Integer.parseInt(js.getString("Code")) == 0) {
						ordernum = js.getString("Data");

						if (typeBankPay.equals("alipay_mobile")) {
//							Pay(money);
						} else if (typeBankPay.equals("Um_mobile")) {
							startSdkToPay(ordernum);
						} else if (typeBankPay.equals("alipaykj_mobile")) {
//							alipaykjPay(money);
						}

					} else {

						String tip = js.getString("Data");
						if (!TextUtils.isEmpty(tip)) {
							if (tip.length() == 38) {
								tip = tip.substring(0, 30);
							}
						}
						Toast.makeText(JddRechargeListActivity.this, tip, Toast.LENGTH_LONG).show();

					}
					showdialog.closeDialog();
				} catch (JSONException ex) {
					// 异常处理代码
					Log.e("error", ex.toString());
					Toast.makeText(JddRechargeListActivity.this, R.string.top_up_failure, Toast.LENGTH_SHORT).show();
				} finally {
					showdialog.closeDialog();
				}
			} else {
				Toast.makeText(JddRechargeListActivity.this, getString(R.string.problem_01), Toast.LENGTH_LONG).show();
			}
		}

		// 5.完成后操作
		protected void onCancelled() {
		}
	}

	public class SendInfoToServerForLog extends AsyncTask<String, Integer, String> {

		String orgStr;
		String afterSign;

		public SendInfoToServerForLog(String org, String afterSign) {
			this.orgStr = org;
			this.afterSign = afterSign;
		}

		@Override
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			try {
				// 设置请求所需参数
				JSONObject param = new JSONObject();
				param.put("original", orgStr);
				param.put("initiate", afterSign);
				// 设置请求action
				String UserID = ApplicationData.getInstance().getUserID();
				GetRequestParams.getRequestUserIDResult(param.toString(), UserID, "42", getApplicationContext());
			} catch (Exception ex) {
				// TODO: handle exception
				Log.e("error", ex.toString());
				return null;
			}
			return null;
		}
	}

	private void checkAlipayORUMValueJMJF(String type) {
		if (!AppUtils.isBlank(laestPay.getText().toString().trim())) {
			Log.d("van", "JMJF----" + getStrMoney);
			try {
				JSONTokener tokener = new JSONTokener(result);
				JSONObject js = (JSONObject) tokener.nextValue();

				// js.put("BuyShare", 0); //原先用固定值0，现改为订单金额
				js.put("SchemeType", 3);
				// js.getInt("Money")+""
				AsyncLoaderJMJF jmjfLoader = new AsyncLoaderJMJF(type, getStrMoney, js);
				jmjfLoader.execute("108");

			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			showToast("请输入金额!");
		}

	}

	// 操作
	private void showLLDilog(String msg) {
		ShowCustomDialog.Builder tDialog = new ShowCustomDialog.Builder(JddRechargeListActivity.this);
		tDialog.setTitle("温馨提示");
		tDialog.setMessage(msg);
		tDialog.setNegativeButton("确定", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
			}

		}).createCustomDialog().show();
	}

	/**
	 * 获取彩票种类名称
	 * 
	 * @param lotID2
	 * @return
	 */
//	private String getLotName(int lotID2) {
//		String name = "";
//		switch (lotID2) {
//		case LOT_ID_SSQ:
//			name = "双色球";
//			break;
//		case LOT_ID_DLT:
//			name = "大乐透";
//			break;
//		case LOT_ID_11X5:
//			name = "11选5";
//			break;
//		case LOT_ID_11YDJ:
//			name = "11运夺金";
//			break;
//		case LOT_ID_CQSSC:
//			name = "重庆时时彩";
//			break;
//		case LOT_ID_FC3D:
//			name = "福彩3D";
//			break;
//		case LOT_ID_QLC:
//			name = "七乐彩";
//			break;
//		case LOT_ID_PL3:
//			name = "排列3";
//			break;
//		case LOT_ID_PL5:
//			name = "排列5";
//			break;
//		case LOT_ID_QXC:
//			name = "七星彩";
//			break;
//		case LOT_ID_ZQ_SFC:
//			name = "胜负彩";
//			break;
//		case LOT_ID_BD:
//			name = "北单";
//			break;
//		case LOT_ID_ZQ_RX9:
//			name = "任选9";
//			break;
//		case LOT_ID_JCZQ:
//			name = "竞彩足球";
//			break;
//		case LOT_ID_JCLQ:
//			name = "竞彩篮球";
//		case LOT_ID_KUAI3:
//			name = "快3";
//			break;
//		case 95:
//			name = "猜冠军";
//			break;
//		}
//		return name;
//	}

	// 调用奖多多的接口
	public class AsyncLoaderJMJF extends AsyncTask<String, Integer, String> {// 继承AsyncTask

		private String typeBankPay;
		private String money;
		private JSONObject js;

		public AsyncLoaderJMJF(String typeBankPay, String money, JSONObject js) {
			super();
			this.typeBankPay = typeBankPay;
			this.money = money;
			this.js = js;
		}

		// 1.准备运行
		protected void onPreExecute() {
			showdialog.showDialog(JddRechargeListActivity.this);
		}

		// 2.正在后台运行
		@Override
		protected String doInBackground(String... params) {
			String strResult;
			try {
				// 设置请求action

				JSONObject param = new JSONObject();
				param.put("BankPay", typeBankPay);
				param.put("PayMoney", money);
				param.put("JMJF", js);

				String action = params[0];
				String UserID = ApplicationData.getInstance().getUserID();
				strResult = GetRequestParams.getRequestUserIDResult(param.toString(), UserID, action,
						getApplicationContext());
				return strResult;
			} catch (Exception ex) {
				// TODO: handle exception
				Log.e("error", ex.toString());
				return null;
			}
		}

		// 3.进度更新
		protected void onProgressUpdate(Integer... progress) {
		}

		// 4.完成后台任务
		protected void onPostExecute(String result) {
			if (result != null) {

				// System.out.println("result = " + result);

				try {
					JSONTokener jsonParser = new JSONTokener(result);
					JSONObject js = (JSONObject) jsonParser.nextValue();
					if (Integer.parseInt(js.getString("Code")) == 0) {
						ordernum = js.getString("Data");

						if (typeBankPay.equals("alipay_mobile")) {
//							Pay(money);
						} else if (typeBankPay.equals("Um_mobile")) {
							// 调用SDK进行支付
							startSdkToPay(ordernum);
						} else if (typeBankPay.equals("alipaykj_mobile")) {
//							alipaykjPay(money);
						}

					} else {
						Toast.makeText(JddRechargeListActivity.this, js.getString("Data"), Toast.LENGTH_SHORT).show();
					}
					showdialog.closeDialog();
				} catch (JSONException ex) {
					// 异常处理代码
					Log.e("error", ex.toString());
					Toast.makeText(JddRechargeListActivity.this, R.string.pay_for_failure, Toast.LENGTH_SHORT).show();
				} finally {
					showdialog.closeDialog();
				}
			} else {
				Toast.makeText(JddRechargeListActivity.this, getString(R.string.problem_01), Toast.LENGTH_LONG).show();
			}
		}

		// 5.完成后操作
		protected void onCancelled() {
		}
	}

}
